#include <iostream>

namespace ABV_ITERATOR {


	// 假设 bar 函数的定义
	int bar(int x) {
		return x;
	}

	// 假设 bar 函数的重载返回指针
	int* bar(int* arr, int x) {
		if (x >= 0 && x < 10) {
			return &arr[x];
		}
		return nullptr; // 返回空指针表示无效访问
	}

	int example1()
	{
		int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int* p;
		for (p = a; p < a + 10; p++)
		{
			if (bar(*p) < 5)
				break;
		}
		return *p;
	}

	int example2(int x)
	{
		int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int* p = bar(a, x); //function returns a pointer to some element in "a"
		if (p < a + 5)
			return *p;

		return p[5]; // DEFECT: p[5] points to a[11] or beyond 
	}


	int example3()
	{
		int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int* p;
		for (p = a; p < a + 10; p++)
		{
			if (bar(*p) < 5)
				break;
		}
		if (p < a + 10)
			return *p;
		return 0;
	}

	int example4(int x)
	{
		int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int* p = bar(a, x); //function returns a pointer to some element in "a"
		if (p >= a + 5)
			return *p;

		return p[5];
	}
}